package com.example.wang7.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.wang7.R;

import com.example.wang7.ui2.jibing.jibingFragment;
import com.example.wang7.ui2.jueyu.jueyuFragment;
import com.example.wang7.ui2.lingyang.lingyangFragment;
import com.example.wang7.ui2.lumao.lumaoFragment;
import com.example.wang7.ui2.weishi.weishiFragment;
import com.google.android.material.tabs.TabLayout;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    private weishiFragment weishiFragment;
    private jibingFragment jibingFragment;
    private lingyangFragment lingyangFragment;
    private jueyuFragment jueyuFragment;
    private lumaoFragment lumaoFragment;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        final TextView textView = root.findViewById(R.id.text_dashboard);
        dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        weishiFragment = weishiFragment.newInstance();
        jibingFragment = jibingFragment.newInstance();
        lingyangFragment = lingyangFragment.newInstance();
        jueyuFragment = jueyuFragment.newInstance();
        lumaoFragment = lumaoFragment.newInstance();
        getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1,weishiFragment).commit();

        TabLayout tabLayout1 = (TabLayout) root.findViewById(R.id.tablayout1);
        tabLayout1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab1) {
                //Toast.makeText(getActivity(),tab.getText(),Toast.LENGTH_SHORT).show();
                if (tab1.getText().equals("喂食")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, weishiFragment).commit();
                }
                if (tab1.getText().equals("疾病")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, jibingFragment).commit();
                }
                if (tab1.getText().equals("领养")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, lingyangFragment).commit();
                }
                if (tab1.getText().equals("绝育")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, jueyuFragment).commit();
                }
                if (tab1.getText().equals("撸猫")) {
                    getChildFragmentManager().beginTransaction().replace(R.id.linearLayout1, lumaoFragment).commit();

                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        return root;
    }
}